//------------------------------------------------------------------------------
//@author czhou 2012 
//------------------------------------------------------------------------------

package mapengine.building {

import flash.display.MovieClip;
import flash.events.Event;

import framework.view.components.graphics.SWF;

import isoengine.IsoSetting;
import isoengine.interfaces.IMapObjectData;
import isoengine.pathing.AStar;
import isoengine.pathing.Node;
import isoengine.world.IsoWorld;

import mapengine.IsoMap;
import mapengine.interfaces.IMapAvatar;

import values.avatar.AvatarValue;

/**
 * MapAvatar
 * @author jliang
 *
 */
public class MapAvatar extends MapObjectBase implements IMapAvatar {
    //==========================================================================
    //  Properties 
    //==========================================================================
    
    protected var path:Array = [];
    
    protected var dirX:int;
    
    protected var dirY:int;
    
    protected var vo:AvatarValue;
    
    //==========================================================================
    //  Constructor 
    //==========================================================================
    
    public function MapAvatar(descriptor:Object = null) {
        
        super(descriptor);
    
    }
    
    //==========================================================================
    //  Protected methods 
    //==========================================================================
    
    override protected function renderHandler(event:Event):void {
        
        checkMove();
    
    }
    
    protected function onMoveComplete():void {
        
        //设置当前地块的walkAble
        //grid.setWalkableByVo(objectData.width, objectData.height, objectData.x, objectData.y, false);
    
    }
    
    protected function checkMove():void {
        		
        //check			
        if(Math.abs(x - ((path[0] as Node).x * IsoSetting.cellSize)) < IsoSetting.avatarMoveSpeed &&
                Math.abs(y - ((path[0] as Node).y * IsoSetting.cellSize)) < IsoSetting.avatarMoveSpeed
		) {
            
			/*
            objectData.x = int(x / IsoSetting.cellSize);
            objectData.y = int(y / IsoSetting.cellSize);            
			*/
			x = (path[0] as Node).x * IsoSetting.cellSize;
			y = (path[0] as Node).y * IsoSetting.cellSize;
            
            
            if(path.length <= 1) {
				x = objectData.x * IsoSetting.cellSize;
				y = objectData.y * IsoSetting.cellSize;
                wait();
                path = [];
                onMoveComplete();
            }
            else {
                checkMoveDirection();
            }
        } 
        
        moveBy(dirX * IsoSetting.avatarMoveSpeed, dirY * IsoSetting.avatarMoveSpeed, 0);
    
    }
    
    protected function checkMoveDirection(shift:Boolean = true):void {
        
        if(shift) path.shift();
        
        dirX = (path[0] as Node).x - int(x / IsoSetting.cellSize);
        dirY = (path[0] as Node).y - int(y / IsoSetting.cellSize);
        
        var dirTemp:int = 0; //临时变量，存储上次转向
        
        if((dirX == -1 && dirY == 1) || (dirX == 0 && dirY == 1)) {
            dirTemp = 0;
        }
        else if((dirX == 1 && dirY == 1) || (dirX == 1 && dirY == 0)) {
            dirTemp = 1;
        }
        else if((dirX == 1 && dirY == -1) || (dirX == 0 && dirY == -1)) {
            dirTemp = 2;
        }
        else if((dirX == -1 && dirY == -1) || (dirX == -1 && dirY == 0)) {
            dirTemp = 3;
        }
        
        if(direction != dirTemp) //如果相等，说明未转向，人物MC不用重新跳转播放
        {
            direction = dirTemp;
        }
    
    }
    
    //==========================================================================
    //  Event handlers
    //==========================================================================
    protected function assetLoadedHandler(swf:SWF):void {
        
        var classRef:Class = swf.getClassRef(vo.skinAsset.classRef);
        var mc:MovieClip = new classRef() as MovieClip;
        initAsset(mc);
        
        wait();
    
    }
    
    //==========================================================================
    //  Public methods 
    //==========================================================================
    
    public function get nextPosX():int {
        /*
        if(path.length > 0) {
            return (path[0] as Node).x;
        }
        else {
            return int(x / IsoSetting.cellSize);
        }
		*/
		return objectData.x;
    }
    
    public function get nextPosY():int {
        /*
        if(path.length > 0) {
            return (path[0] as Node).y;
        }
        else {
            return int(y / IsoSetting.cellSize);
        }
    	*/
		return objectData.y;
    }
    
    public function get avatarValue():AvatarValue {
        
        return vo;
    
    }
    
    //==========================================================================
    //  Overridden methods: MapObjectBase
    //==========================================================================
    override public function set objectData(value:IMapObjectData):void {
        
        super.objectData = value;
        
		objectData.walkable = false;		
		
        vo = objectData as AvatarValue;
		
    
    }
    
    override public function initialize():void {
        
        loadAsset(vo.skinAsset.url, assetLoadedHandler);
    
    }
    
    public function wait():void {
        
        playAnimation("walk");
        stopRender();
    
    }
    
    public function walkTo(dx:int, dy:int):void {
        
		path = [];
		x = objectData.x * IsoSetting.cellSize;
		y = objectData.y * IsoSetting.cellSize;
		
		grid.resetWalkable();
		
		grid.getNode(objectData.x, objectData.y).walkable = true;
		grid.getNode(nextPosX, nextPosY).walkable = true;
        
        grid.setEndNode(dx, dy);
        grid.setStartNode(nextPosX, nextPosY);
        
        var _Astar:AStar = new AStar(); //寻路	
        _Astar.setAstarType(true);
        
        if(_Astar.findPath(grid)) {			
        	
			/*
           var newPath:Array = _Astar.path.slice();
           if(path.length > 0)
           {
           	path = [path.shift()];
           }
           path = path.concat(newPath);
         	*/			
	        path = _Astar.path.slice();			
	        playAnimation("run");
			
			objectData.x = dx;
			objectData.y = dy;
			
			checkMoveDirection();
			
	        startRender();			
            
        }
		
    
    }
}
}



